<?php
namespace app\index\controller;

use app\index\BaseController;
use app\model\User;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    //model
    public function test()
    {
        //增删改查1
        $userModel = new User();
        $userModel->name = "test2";
        $userModel->age = "13";
        $userModel->gender = 2;
        $userModel->phone = "18994318501";
        $userModel->passwd = "2222222222222";
        $r = $userModel->save();
var_dump($r);
        //增删改查2



    }

    public function test2()
    {
        //$user2 = User::find(3);
        $user2 = User::where('phone','18994318503')->find();
        $user2->name = "shanjinlin";
        $r = $user2->save();
        var_dump($r);
    }

    public function test3()
    {
        //$user2 = User::find(3);
        $users = User::scope('age', 25)->select();
var_dump($users->toArray());
        foreach ($users as $user)
        {
            $user->passwd = 'thisispassword';
            $user->save();
        }

        return "这是批量更新";
    }

    public function dictionary(){
        $keyword=$this->request->param('keyword');
        if (!empty($keyword)){
            if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$keyword)) {
                //中文
                $data = Db::query("SELECT * FROM (SELECT
                                    TABLE_SCHEMA as datebase,
                                    TABLE_NAME as tab_name,
                                    COLUMN_NAME,
                                    COLUMN_COMMENT as tab_cname
                                FROM
                                    information_schema. COLUMNS
                                WHERE
                                  TABLE_SCHEMA in('shop', 'wuliu') 
                                  GROUP BY TABLE_NAME ) aa WHERE aa.tab_cname like '%{$keyword}%'");
            } else {
                //英文
                $data = Db::query("SELECT
                                    TABLE_SCHEMA as datebase,
                                    TABLE_NAME as tab_name,
                                    COLUMN_NAME,
                                    COLUMN_COMMENT as tab_cname
                                FROM
                                    information_schema. COLUMNS
                                WHERE
                                    TABLE_SCHEMA in('shop', 'wuliu') 
                                 AND TABLE_NAME like '%{$keyword}%' GROUP BY TABLE_NAME");
            }

            View::assign([
                'data' => $data,
                'keyword' => $keyword
            ]);

        }else{

            $data=Db::query("SELECT
                                TABLE_SCHEMA as datebase,
                                TABLE_NAME as tab_name,
                                COLUMN_NAME,
                                COLUMN_COMMENT as tab_cname
                            FROM
                                information_schema. COLUMNS
                            WHERE
                              TABLE_SCHEMA in( 'shop', 'wuliu') 
                             GROUP BY TABLE_NAME");
            View::assign([
                'data' => $data,
                'keyword' => $keyword
            ]);
        }
        return View::fetch('dictionary');
    }

    public function dictionary2(){
        $tab_name = $this->request->param('tab_name');
        $row = Db::connect("mysql")->query("SELECT TABLE_SCHEMA as datebase, TABLE_NAME as tab_name, COLUMN_NAME as name, COLUMN_COMMENT as note
                    FROM
                        information_schema. COLUMNS
                    WHERE
                        TABLE_SCHEMA in( 'shop', 'wuliu') 
                    AND TABLE_NAME='{$tab_name}'");
        //$data["data"]=$row;
        return json([
            'success' => true,
            'data' => $row
        ]);
    }
}
